<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: staff_adtracking.php,v $ : $Revision: 1.8 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

if ($_SWIFT["staff"]["ls_entab"] == "0")
{
	echo '<font face="Verdana, Arial, Helvetica" size="2" color="red"><b>'.$_SWIFT["language"]["nopermission"].'</b></font>';
	exit;
}

require_once ("./includes/functions_html.php");
require_once ("./modules/livesupport/functions_adtracking.php");
$grid = new Grid();
$template->loadLanguageSection("livesupport2");

$interface->assignQuickLink($_SWIFT["language"]["managecampaigns"], "index.php?_m=livesupport&_a=adtracking", "icon_campaigns.gif");
$interface->assignQuickLink($_SWIFT["language"]["insertcampaign"], "index.php?_m=livesupport&_a=insertcampaign", "icon_insertcampaign.gif");
$interface->assignQuickLink($_SWIFT["language"]["reports"], "index.php?_m=livesupport&_a=adreports", "icon_adreports.gif");


/**
* ###############################################
* FUNCTION DECLARATIONS
* ###############################################
*/

/**
* Callback for parsing campaigns
*/
function _campaignFields($arg)
{
	global $_SWIFT;
	$record = &$arg[0];
	$fields = &$arg[1];

	$record["title"] = '<table width="100%"  border="0" cellspacing="0" cellpadding="0"><tr><td width="1" align="left"><img src="'. $_SWIFT["themepath"] .'icon_campaign.gif" /></td><td><span class="smalltext">&nbsp;<a href="index.php?_m=livesupport&_a=editcampaign&campaignid='. $record["campaignid"] .'" title="'. $_SWIFT["language"]["edit"] .'">'.htmlspecialchars($record["title"]).'</a></span></td></tr></table>';
	$record["redirecturl"] = $record["redirecturl"].'&nbsp;[<a href="'. $record["redirecturl"] .'" target="_blank">^</a>]';

	$campaignurl = $_SWIFT["swiftpath"]."visitor/index.php?_m=livesupport&_a=track&campaignid=".intval($record["campaignid"]);
	$record["campaignurl"] = '<a href="javascript:;" onClick="return overlib(\''.$_SWIFT["language"]["campaignurlinfo"]."<BR /><BR />". $campaignurl .'\', CAPTION, \''. $_SWIFT["language"]["campaignurl"] .'\', STICKY, FOLLOWMOUSE, WIDTH, 300, FGCLASS, \'stickyfg\', BGCLASS, \'stickybg\', TEXTFONTCLASS, \'stickyfont\', CAPTIONFONTCLASS, \'stickyfont\', CLOSEFONTCLASS, \'stickyfont\', EXCLUSIVE, CLOSECLICK);">'.$_SWIFT["language"]["view"].'</a>&nbsp;[<a href="'. $campaignurl .'" target="_blank">^</a>]';

	$record["options"] = '<a href="index.php?_m=livesupport&_a=editcampaign&campaignid='. $record["campaignid"] .'" title="'. $_SWIFT["language"]["edit"] .'"><img src="'. $_SWIFT["themepath"] .'icon_edit.gif" border="0">&nbsp;'. $_SWIFT["language"]["edit"] .'</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onClick="javascript:doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=livesupport&_a=adtracking&do=delete&campaignid='. $record["campaignid"] .'\');" href="#" title="'. $_SWIFT["language"]["delete"] .'"><img src="'. $_SWIFT["themepath"] .'icon_delete.gif" border="0">&nbsp;'. $_SWIFT["language"]["delete"] .'</a>&nbsp;';
	return $record;
}

/**
* Mass Action: Delete Campaigns
*/
function _maDeleteCampaigns()
{
	global $_SWIFT, $departments;

	if (!is_array($_POST["itemid"]))
	{
		return false;
	}

	deleteCampaigns($_POST["itemid"]);
}


/**
* ###############################################
* MANAGE CAMPAIGNS
* ###############################################
*/
if ($eventaction == "adtracking")
{
	if ($_GET["do"] == "delete" && $_GET["campaignid"] != "")
	{
		$infomessage = $_SWIFT["language"]["campaigndelconfirm"];
		deleteCampaigns(array($_GET["campaignid"]));
	}

	if (trim($_REQUEST["campaigninsert"]) != "")
	{
		$infomessage = sprintf($_SWIFT["language"]["campaigninsertconfirm"], htmlspecialchars($_REQUEST["campaigninsert"]));
	} else if (trim($_REQUEST["campaignupdate"]) != "") {
		$infomessage = sprintf($_SWIFT["language"]["campaignupdateconfirm"], htmlspecialchars($_REQUEST["campaignupdate"]));
	}
	
	$options["recordsperpage"] = "6";
	$options["sortby"] = "title";
	$options["sortorder"] = "asc";
	$options["massaction"][0]["title"] = $_SWIFT["language"]["delete"];
	$options["massaction"][0]["callback"] = "_maDeleteCampaigns";

	$options["idname"] = "campaignid";
	$options["quicksearch"] = true;

	$options["advancedsearch"][0]["title"] = $_SWIFT["language"]["title"];
	$options["advancedsearch"][0]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'adcampaigns` WHERE title LIKE \'%$_searchstr%\' $_sortjoin;';
	$options["advancedsearch"][1]["title"] = $_SWIFT["language"]["redirecturl"];
	$options["advancedsearch"][1]["query"] = 'SELECT * FROM `'. TABLE_PREFIX .'adcampaigns` WHERE redirecturl LIKE \'%$_searchstr%\' $_sortjoin;';

	$options["callback"] = "_campaignFields";

	$fields[0]["name"] = "title";
	$fields[0]["title"] = $_SWIFT["language"]["title"];
	$fields[0]["width"] = "";

	$fields[1]["name"] = "campaignurl";
	$fields[1]["title"] = $_SWIFT["language"]["campaignurl"];
	$fields[1]["width"] = "150";
	$fields[1]["align"] = "center";
	$fields[1]["type"] = "custom";

	$fields[2]["name"] = "redirecturl";
	$fields[2]["title"] = $_SWIFT["language"]["redirecturl"];
	$fields[2]["width"] = "330";
	$fields[2]["align"] = "center";

	$fields[3]["type"] = "custom";
	$fields[3]["name"] = "options";
	$fields[3]["title"] = $_SWIFT["language"]["options"];
	$fields[3]["width"] = "160";
	$fields[3]["align"] = "center";

	$grid = new Grid();

	$template->assign("overlib", true);

	$interface->staffHeader($_SWIFT["language"]["managecampaigns"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=adtracking" title="'.$_SWIFT["language"]["managecampaigns"].'">'.$_SWIFT["language"]["managecampaigns"]."</a>", $_SWIFT["language"]["desc_adtrack"], 3);

	printInfoBox($infomessage);

	$grid->start("adtracking", $_SWIFT["language"]["campaignlist"], 'SELECT * FROM `'. TABLE_PREFIX .'adcampaigns` $_sortjoin;', 'SELECT COUNT(*) AS totalitems FROM `'. TABLE_PREFIX .'adcampaigns`;', 'SELECT * FROM `'. TABLE_PREFIX .'adcampaigns` WHERE title LIKE \'%$_searchstr%\' $_sortjoin;', $fields, $options);
	
	$grid->display("adtracking");

	$interface->staffFooter();






/**
* ###############################################
* INSERT NEW CAMPAIGN
* ###############################################
*/
} else if ($eventaction == "insertcampaign") {
	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["redirecturl"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			// Insert the new campaign

			insertCampaign($_POST["title"], $_POST["redirecturl"]);

			printRedirect(sprintf($_SWIFT["language"]["campaigninsertconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=livesupport&_a=adtracking&campaigninsert=".urlencode($_POST["title"]));
			exit;
		}		
	}

	$interface->staffHeader($_SWIFT["language"]["managecampaigns"]." &gt; ".$_SWIFT["language"]["insertcampaign"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=adtracking" title="'.$_SWIFT["language"]["campaigns"].'">'.$_SWIFT["language"]["campaigns"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=insertcampaign" title="'.$_SWIFT["language"]["insertcampaign"].'">'.$_SWIFT["language"]["insertcampaign"]."</a>", $_SWIFT["language"]["desc_adtrack"], 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCampaignForm(INSERT);

	$template->assign("backurl", "index.php?_m=livesupport&_a=adtracking");

	$interface->staffFooter();




/**
* ###############################################
* EDIT CAMPAIGN
* ###############################################
*/
} else if ($eventaction == "editcampaign") {
	$_campaign = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."adcampaigns` WHERE `campaignid` = '". intval($_REQUEST["campaignid"]) ."';");
	if (empty($_campaign["campaignid"]))
	{
		trigger_error($_SWIFT["language"]["invalidcampaign"], E_USER_ERROR);
	}

	if ($_POST["step"] == 1)
	{
		if (trim($_POST["title"]) == "" || trim($_POST["redirecturl"]) == "")
		{
			$errormessage = $_SWIFT["language"]["requiredfieldempty"];
		} else {
			// Update Campaign

			updateCampaign($_POST["campaignid"], $_POST["title"], $_POST["redirecturl"]);

			printRedirect(sprintf($_SWIFT["language"]["campaignupdateconfirm"], htmlspecialchars($_POST["title"])), "index.php?_m=livesupport&_a=adtracking&campaignupdate=".urlencode($_POST["title"]));
			exit;
		}
	}

	$_POST = array_merge($_POST, $_campaign);

	$template->assign("itemoptiontitle", $_SWIFT["language"]["adoptions"]);
	$interface->assignItemOption($_SWIFT["language"]["deletecampaign"], "", "icon_delete.gif", "", 'doConfirm(\''. $_SWIFT["language"]["actionconfirm"] .'\', \'index.php?_m=livesupport&_a=adtracking&do=delete&campaignid='. $_campaign["campaignid"] .'\')');

	$interface->staffHeader($_SWIFT["language"]["managecampaigns"]." &gt; ".$_SWIFT["language"]["editcampaign"], 3);

	$interface->staffNavBar('<a href="index.php?_m=livesupport&_a=adtracking" title="'.$_SWIFT["language"]["campaigns"].'">'.$_SWIFT["language"]["campaigns"].'</a> &raquo; <a href="index.php?_m=livesupport&_a=editcampaign&campaignid='. intval($_campaign["campaignid"]) .'" title="'.$_SWIFT["language"]["editcampaign"].'">'.$_SWIFT["language"]["editcampaign"]."</a>", $_SWIFT["language"]["desc_adtrack"], 3);

	printInfoBox($infomessage);
	printErrorBox($errormessage);

	renderCampaignForm(EDIT);

	$template->assign("backurl", "index.php?_m=livesupport&_a=adtracking");

	$interface->staffFooter();







/**
* ###############################################
* AD TRACKING REPORTS
* ###############################################
*/
} else if ($eventaction == "adreports") {

	require_once ("./modules/livesupport/report_adtracking.php");




}
?>